<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastSuccess, ToastWarning, Toastloading, allNumber } from '@/utils/public';
  import serachGift from '@/components/serachGift/index.vue';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    disabled:false,
    title:'',
    gameTypeList:[] as Array<any>,
  })
  interface formQuert {
    id:number
    custom_id:number
    gift_ids:string
    diamond:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    gift_ids: [{ required: true, message: "请输入盖楼礼物ID", trigger: "blur" }],
    diamond: [{ required: true, message: "请输入单层钻石数", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      let submitSet = false
      const { id } = form
      if(id) {
        await gamesApi.customBridgeEdit(form).then(()=>{
          ToastSuccess('编辑成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      else {
        await gamesApi.customBridgeAdd(form).then(()=>{
          ToastSuccess('新增成功')
          submitSet = true
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        $emit('updateList')
        closeDialog()
      }
    })
  }
  const openDialog = async(query:any={})=> {
    const {
      id=0, custom_id=0, game_name='', gift_ids='', one_level_diamond:diamond=''
    } = query
    if(!custom_id) return ToastWarning('缺少活动ID')
    const obj = {
      custom_id, gift_ids, diamond,
    } as any
    if(id) obj['id'] = id
    Object.assign(form,obj)
    action.dialogFormVisible = true
    action.title = `活动：${game_name}${id?'编辑盖楼配置':'创建盖楼配置'}`
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="50%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="单层钻石数" prop="diamond">
          <el-input
            :disabled="action.disabled"
            v-model="form.diamond"
            placeholder="请输入单层钻石数"
            @input="allNumber(form,'diamond')"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="盖楼礼物" prop="gift_ids">
          <el-input
            :disabled="action.disabled"
            v-model="form.gift_ids"
            :autosize="{ minRows: 4, maxRows: 4}"
            type="textarea"
            placeholder="请输入盖楼礼物ID英文逗号分割：例：666,888,999"
          />
          <div class="tipsInput">请输入盖楼礼物ID英文逗号分割：例：666,888,999</div>
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>